﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StockTrading
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly Model.ApplicationContext _context = new Model.ApplicationContext();
        public MainWindow()
        {
            InitializeComponent();

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //try yo migrate database
                _context.Database.Migrate();
            }
            catch (Exception)
            {

            
            }
            // this is for demo purposes only, to make it easier
            _context.Database.EnsureCreated();
            // load the entities into EF Core
            _context.Users.Load();
            //查询root
            var user = _context.Users.Where(x => x.LoginName == "root").FirstOrDefault();
            if (user == null)
            {
                //如果root不存在，添加root账号
                _context.Users.Add(new User()
                {
                    Name = "root",
                    LoginName = "root",
                    Password = "admin123...",
                    IsAdmin = true,
                    CnyFree = 9999999999999,
                    CnyFreezed = 0
                });
                 _context.SaveChanges();
               
            }
        }

        private void Close_Closing(object sender, CancelEventArgs e)
        {
            _context.Dispose();
        }

        private async void BtnTest_Click(object sender, RoutedEventArgs e)
        {
             Test();
            /* //获取用户名
             var username = this.UserName.Text;
             //获取密码
             var password = this.Password.Password;
             //查询对应用户名和密码
             bool isexist = await _context.Users
                             .AsNoTracking()
                             .AnyAsync(x => x.Password == password && x.LoginName == username);
             if (!isexist) MessageBox.Show("用户名或密码错误");
             else MessageBox.Show("登录成功");*/

        }
        private void Test()
        {
             _context.Users.Add(new User()
            {
                Name = "崔翔",
                LoginName = "cui",
                Password = "1111111",
                IsAdmin = true,
                CnyFree = 10000,
                CnyFreezed = 0,
            });
             _context.Users.Add(new User()
            {
                Name = "陈小潘",
                LoginName = "cui",
                Password = "1111111",
                IsAdmin = true,
                CnyFree = 10000,
                CnyFreezed = 0,
            });
            _context.SaveChanges();
             _context.Stocks.Add(new Model.Stock()
            {
                Name = "河大股份"
            });

             _context.SaveChanges();
              _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 2,
                StockId = 1,
                Type = Model.Order.OrderType.Sell,
                Price = 12,
                Undealed = 200,
                Dealed = 0,
                Canceled = 0,
            });
            _context.SaveChanges();
            _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 2,
                StockId = 1,
                Type = Model.Order.OrderType.Sell,
                Price = 13,
                Undealed = 300,
                Dealed = 0,
                Canceled = 0,
            });
            _context.SaveChanges();

            _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 3,
                StockId = 1,
                Type = Model.Order.OrderType.Buy,
                Price = 12.5M,
                Undealed = 200,
                Dealed = 0,
                Canceled = 0,
            });
            _context.SaveChanges();

            _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 3,
                StockId = 1,
                Type = Model.Order.OrderType.Buy,
                Price = 13,
                Undealed = 100,
                Dealed = 0,
                Canceled = 0,
            });
            _context.SaveChanges();

            _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 2,
                StockId = 1,
                Type = Model.Order.OrderType.Buy,
                Price = 12,
                Undealed = 200,
                Dealed = 0,
                Canceled = 0,
            });
            _context.SaveChanges();

            _context.Orders.Add(new Model.Order()
            {
                CreateDate = System.DateTime.Now,
                UserId = 2,
                StockId = 1,
                Type = Model.Order.OrderType.Buy,
                Price = 13.1M,
                Undealed = 200,
                Dealed = 0,
                Canceled = 0,
            });
              _context.SaveChanges();
        }
    }
}

